package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TbBrandService;
import com.dongdongshop.service.TbSpecificationService;
import com.dongdongshop.service.TbTypeTemplateService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("template")
public class TbTypeTemplateController {


    //打印日志
    private Logger logger = LoggerFactory.getLogger(TbBrandController.class);

    @DubboReference
    public TbTypeTemplateService templateService;

    @DubboReference
    private TbBrandService brandService;

    @DubboReference
    private TbSpecificationService specService;

    @RequestMapping("show")
    public String show(){
        return "admin/type_template";
    }

    @RequestMapping("listTemplate")
    @ResponseBody
    public Result listTemplate(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "3") Integer pageSize,
                               String name){
        try {
            PageResult templates = templateService.listTemplate(pageNum,pageSize,name);
            return Result.OK().setData(templates);
        }catch (Exception e){
            logger.info("查询模板出现异常: {}",e);
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }
    @RequestMapping("listBrand")
    @ResponseBody
    public Result listBrand(){
        try {
            List<TbBrand> listBrand = brandService.listBrandsAll();
            return Result.OK().setData(listBrand);
        }catch(Exception e){
            logger.info("查询brand出现异常: {}",e);
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }
    @RequestMapping("listSpec")
    @ResponseBody
    public Result listSpec(){
        try {
            List<TbSpecification> listSpec = specService.listSpecAll();
            return Result.OK().setData(listSpec);
        }catch(Exception e){
            logger.info("查询spec出现异常: {}",e);
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }
    @RequestMapping("addTemplate")
    @ResponseBody
    public Result addTemplate(TbTypeTemplate tbTypeTemplate){
        try {
            templateService.addTemplate(tbTypeTemplate);
            return Result.OK();
        }catch(Exception e){
            logger.info("增加模板出现异常: {}",e);
            return Result.response(ResultEnum.ADD_MESSAGE_ERROE);
        }
    }
    @RequestMapping("toUpdate")
    @ResponseBody
    public Result toUpdate(Long id){
        try {
            TbTypeTemplate template = templateService.selectTemplate(id);
            return Result.OK().setData(template);
        }catch(Exception e){
            logger.info("回显模板出现异常: {}",e);
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }
    @RequestMapping("updateTemplate")
    @ResponseBody
    public Result updateTemplate(TbTypeTemplate tbTypeTemplate){
        try {
            templateService.updateTemplate(tbTypeTemplate);
            return Result.OK();
        }catch(Exception e){
            logger.info("修改模板出现异常: {}",e);
            return Result.response(ResultEnum.UPDATE_MESSAGE_ERROE);
        }
    }
    @RequestMapping("deleteTemplates")
    @ResponseBody
    public Result deleteTemplates(Long[] ids){
        try {
            templateService.deleteTemplates(ids);
            return Result.OK();
        }catch(Exception e){
            logger.info("删除模板出现异常: {}",e);
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }
}
